package com.sunlong.jiangwei.service.impl;

import com.sunlong.jiangwei.dao.JwCategoryMapper;
import com.sunlong.jiangwei.entity.JwCategory;
import com.sunlong.jiangwei.service.IJwCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 孙龙
 * @since 2021-02-19
 */
@Service
public class JwCategoryServiceImpl implements IJwCategoryService {

    @Autowired
    private JwCategoryMapper categoryMapper;


    @Override
    public List<JwCategory> findAll() {
        List<JwCategory> categories = categoryMapper.selectAll();
        for (int i = 0; i < categories.size(); i++) {
            categories.get(i).setShow(false);
        }
        return categories;
    }

    @Override
    public void deleteById(long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void saveOrUpdateCategory(JwCategory category) {
        if (category.getCategoryId() == -1){
            JwCategory jwCategory = new JwCategory();
            jwCategory.setCategoryName(category.getCategoryName());
            categoryMapper.insert(jwCategory);
        }else {
            JwCategory category1 = categoryMapper.selectByPrimaryKey(category.getCategoryId());
            if(category1.getCategoryName().equals(category.getCategoryName()))return;
            categoryMapper.updateByPrimaryKey(category);
        }
    }
}
